/******************* 公共变量 *******************/
var urlData = core.getUrlData(['activity_id', 'type']),
	takePhoto,
	vm;

/******************* 公共函数 *******************/
function hintData(data) {
	console.log(data)
	if (data.status == 1) {
		core.hintBack(data.message);
	} else if (data.result == 1) {
		core.hintBack(data.message);
	} else {
		vm.btnJudge = true;
		core.layerHint(data.message);
	}
}

/******************* 微信配置 *******************/
wx.ready(function () {
	wx.checkJsApi({  
		jsApiList : ['getLocation', 'uploadImage','previewImage','chooseImage','scanQRCode','startRecord','stopRecord','onVoiceRecordEnd','playVoice','onVoicePlayEnd','onMenuShareAppMessage'],  
		success : function(res) {  
		},
		complete: function (data) {
			if (data.checkResult.getLocation == 'no') {
				useGps(true);
			} else {
				useGps();
			}
		}
	});

	takePhoto = function () {
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			// sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				console.log(res)
				// vm.base64 = res.localIds[0];
				wx.uploadImage({
					localId: res.localIds[0], // 需要上传的图片的本地ID，由chooseImage接口获得
					isShowProgressTips: 1, // 默认为1，显示进度提示
					success: function (res) {
						$.getJSON(saveWxImageUrl,{media_id: res.serverId}, function(data) {
							console.log(data)
							vm.base64 = data.image_url;
							vm.wxImage = data.image_url;
						});

					}
				});
				
			}
		});
	}
});

function useGps(useBaidu) {
	/******************* Vue初始化代码 *******************/
	vm = new Vue({
		el: '.wrap',
		data: {
			base64: updateImg,
			encodeBase64: '',
			type: urlData[1],
			id: urlData[0],
			btnJudge: true,
			wxImage: ''
		},
		methods: {
			wxPhoto: function () {
				takePhoto();
			},
			// scan: function() {
			// 	var _this = this;
			// 	if (/^image\/\w+/.test(document.getElementById('scan').files[0].type)) {
			// 		lrz(document.getElementById('scan').files[0], {
			// 			width: 500,
			// 			quality: 0.8
			// 		}).then(function(data) {
			// 			_this.base64 = data.base64;
			// 			_this.encodeBase64 = encodeURIComponent(data.base64);
			// 		});
			// 	} else {
			// 		core.layerHint('请选择图片文件');
			// 	}
			// },
			update: function() {
				var _this = this;
				if (_this.wxImage) {
					if (_this.btnJudge) {
						_this.btnJudge = false;
						$.ajax({
							url: faceUrl,
							type: 'post',
							data: {
								image: _this.wxImage
							},
							success: function(data) {
								console.log(data)

								if (data.result == 1) {
									if (_this.type == 'register') {
										core.setCookie('scan=1', 3600000);
										$.ajax({
											url: registerUrl,
											type: 'post',
											data: {
												id: _this.id
											},
											success: function(data) {
												if (data.status == 1) {
													core.hintBack(data.message);
												} else if (data.result == 1) {
													core.hintBack(data.message);
												} else if ((data.status == 2) || (data.result == 2)) {
													core.hintHref(data.message, invite_cash + '?res_id=0');
												} else {
													vm.btnJudge = true;
													core.layerHint(data.message);
												}
											}
										});
									} else if (_this.type == 'arrive') {
										var bdMap = core.bdMap({
											gpsDetail: function(data) {
												$.ajax({
													url: arriveUrl,
													type: 'post',
													data: {
														id: _this.id,
														lng: data.result.location.lng,
														lat: data.result.location.lat
													},
													success: function(data) {
														hintData(data);
													}
												});
											}
										}, useBaidu);
									} else if (_this.type == 'scan') {
										$.ajax({
											url: scanHongbaoUrl,
											type: 'post',
											data: {
												id: _this.id
											},
											success: function(data) {
												hintData(data);
											}
										});
									}
								} else {
									_this.base64 = updateImg;
									_this.btnJudge = true;
									_this.wxImage = '';
									core.layerHint(data.message);
								}
							}
						});
					}
				} else {
					core.layerHint('请上传照片~');
				}
			}
		},
		computed: {

		},
		components: {

		},
		created: function() {

		},
		mounted: function() {
			// common.bindBack();
			$('title').html((this.type == 'register') ? '刷脸报名' : ((this.type == 'arrive') ? '刷脸到达' : '刷脸领红包'));
		},
		updated: function () {
			core.centerImg('html body .wrap .activity-scan .update-img img');
		}
	});
}
